package com.china08.yxyapi.api.comm.upload;

import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.util.YXCSPUtil;

@CrossOrigin
@RestController("UploadFileController")
public class UploadFileController {

    @Autowired
    private YXCSPUtil yxcspUtil;

    /**
     * 上传文件
     * 
     * @throws IOException
     */
    @RequestMapping(value = "/api_yw/comm/uploadFile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel upLoadWenDoc(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        RespModel respModel = new RespModel();

        Assert.isTrue((file != null), "为选择文件");

        String name = file.getOriginalFilename();
        Assert.isTrue(StringUtils.isNotBlank(name), "文件没有名称");

        String ext = name.substring(name.lastIndexOf(".") + 1, name.length());
        String fileType = "doc,docx,ppt,pptx,xls,xlsx,pdf,txt,zip,png,bmp,jpg,gif,JPG,JPEG";
        Assert.isTrue((Arrays.asList(fileType.split(",")).contains(ext)), "文件类型错误");
        String md5 = DigestUtils.md5Hex(file.getBytes());
        long size = file.getSize();
        String title = name.substring(0, name.indexOf("."));
        String url = yxcspUtil.push(file);
        respModel.setName(name);
        respModel.setMd5(md5);
        respModel.setSize(size);
        respModel.setTitle(title);
        respModel.setExt(ext);
        respModel.setUrl(url);
        respModel.setMessage("上传成功");
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {

        // 文件名称
        public String name;
        // 文件md5
        public String md5;
        // 文件 大小
        public long size;
        // 文件标题
        public String title;
        // 文件后缀
        public String ext;
        // 文件url
        public String url;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getMd5() {
            return md5;
        }

        public void setMd5(String md5) {
            this.md5 = md5;
        }

        public long getSize() {
            return size;
        }

        public void setSize(long size) {
            this.size = size;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getExt() {
            return ext;
        }

        public void setExt(String ext) {
            this.ext = ext;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

    }
}
